﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text.RegularExpressions;

public partial class my_new_edit : AdminpageBase
{
    protected string ctype = ReadRequest.RequestString("t");
    protected long id = ReadRequest.RequestLong("id");
    protected Model.GoldCode model = new Model.GoldCode();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            inidata();
        }
    }
    private bool IsNumber(string value)
    {
        // return System.Text.RegularExpressions.Regex.IsMatch(value, @"^[A-Za-z]+$");
        return Regex.Match(value, "^\\d+$").Success;
    }
    private int IsExit(string code)
    {
        Model.GoldCode model = BLL.GoldCode.GetModelbycode(code);
        return model != null ? model.Id : 0;
    }
    private void inidata()
    {
        try
        {
            if (ctype != null && "edit".Equals(ctype))
            {
                model = BLL.GoldCode.GetModel(id);
                if (model != null)
                {
                    codetxt.Text = model.Code;
                    goldcouttxt.Text = model.Money.ToString();
                    titletxt.Text = model.Title.ToString();
                    passtxt.Text = model.Password.ToString();
                    endtimetxt.Text = model.EndTime.ToString();
                    if (model.Type == 1)
                    {
                        rdbcode.Checked = true;
                    }
                    else if (model.Type == 2)
                    {
                        rdbccaijin.Checked = true;
                    }
                }
            }
            else
            {
                ctype = "ad";
            }
        }
        catch
        {
            ctype = "ad";
        }
    }

    //提交按钮事件
    protected void btn_submit_Click(object sender, EventArgs e)
    {
        save_Newscont();
    }
    private void save_Newscont()
    {
        //获取登录信息
        Model.Manageadmin loginUserInfo_cur = BLL.CMember.ManageadminLoginInfo();
        //没有登录
        if (loginUserInfo_cur == null)
        {
            alertmessageAndGoback("保存失败");
            return;
        }
        bool isadd = true;
        if (ctype != null && "ad".Equals(ctype.ToLower()))
        {
            isadd = true;
        }
        else
        {
            isadd = false;
        }
        if (isadd)
        {

            Model.GoldCode model = new Model.GoldCode();
            model.Title = titletxt.Text.ToString();
            if (IsExit(codetxt.Text) > 0)
            {
                alertmessageAndGoback("兑换码已存在请重新输入");
                return;
            }
         

            model.Code = codetxt.Text;
            model.Password = passtxt.Text.ToString();
            if (!IsNumber(goldcouttxt.Text))
            {
                alertmessageAndGoback("兑换金币数必输数字");
                return;
            }
            model.Money = decimal.Parse(goldcouttxt.Text);
            model.RemainMoney = decimal.Parse(goldcouttxt.Text);
            model.Addtime = DateTime.Now;
            model.IsExchange = false;

            model.EndTime = Convert.ToDateTime(endtimetxt.Text.ToString());

            if (rdbcode.Checked)
            {
                model.Type = 1;
            }
            else if (rdbccaijin.Checked)
            {
                model.Type = 2;
            }


            if (BLL.GoldCode.Add(model) > 0)
            {
                alertmessageAndGo("保存成功！", "/manage/gold/gold.aspx");
                return;
            }
            else
            {
                alertmessageAndGoback("保存失败");
                return;
            }
        }
        else
        {
            Model.GoldCode model = BLL.GoldCode.GetModel(id);
            if (model != null)
            {
                model.Title = titletxt.Text.ToString();
                if (IsExit(codetxt.Text) > 0)
                {
                    alertmessageAndGoback("兑换码已存在请重新输入");
                    return;
                }
               
                model.Code = codetxt.Text;
                model.Password = passtxt.Text.ToString();
                if (!IsNumber(goldcouttxt.Text))
                {
                    alertmessageAndGoback("兑换金币数必输数字");
                    return;
                }
                model.Money = decimal.Parse(goldcouttxt.Text);
                model.Addtime = DateTime.Now;
                model.IsExchange = false;

                model.EndTime = Convert.ToDateTime(endtimetxt.Text.ToString());

                if (rdbcode.Checked)
                {
                    model.Type = 1;
                }
                else if (rdbccaijin.Checked)
                {
                    model.Type = 2;
                }
                if (BLL.GoldCode.Update(model) > 0)
                {
                    alertmessageAndGo("保存成功！", "/manage/gold/gold.aspx");
                    return;
                }
                else
                {
                    alertmessageAndGoback("保存失败");
                    return;
                }
            }
        }
    }
}